<?php
// +----------------------------------------------------------------------
// | 管理员控制器
// +----------------------------------------------------------------------
// | Author: myh
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\service\SysUserService;

class SysUser extends AdminController
{
    //新增
    public function add(){
        $data = $this->request->post();
        //验证规则
        $validate = [
            'account' => 'require',
            'password'  => 'require|regex:/^[a-zA-Z0-9_]{8,20}$/',
        ];
        //提示信息
        $message = [
            'account.require' => '账号不能为空!',
            'password.require' => '密码不能为空!',
            'password.regex' => '密码长度8~20位，包含字母数字下划线!',
        ];
        $this->validate($data, $validate, $message);

        return SysUserService::save($data);
    }

    //更新
    public function edit(){
        $data = $this->request->post();
        //验证规则
        $validate = [
            'id' => 'require',
            'account' => 'require',
        ];
        //提示信息
        $message = [
            'id.require' => 'ID不能为空!',
            'account.require' => '账号不能为空!',
        ];

        if(!empty($data['password'])){
            //如果密码不为空时，说明是修改密码，因此要进行密码校验
            $validate['password'] = 'require|regex:/^[a-zA-Z0-9_]{8,20}$/';
            $message['password.require'] = '密码不能为空!';
            $message['password.regex'] = '密码长度8~20位，包含字母数字下划线!';
        }else{
            //销毁变量
            unset($data['password']);
        }

        $this->validate($data, $validate, $message);

        return SysUserService::update($data);
    }

    //列表
    public function page(){
        return SysUserService::page($this->request->get());
    }

    //登录用户信息
    public function loginUserInfo(){
        return SysUserService::info(getUserId());
    }

    //启用、禁用
    public function updateStatus(){
        $data = $this->request->post();
        //验证规则
        $validate = [
            'id'  => 'require',
            'status'  => 'require'
        ];
        //提示信息
        $message = [
            'id.require' => '请选择要禁用的数据!',
            'status.require' => '状态不能为空!',
        ];

        $$this->validate($data, $validate, $message);

        return SysUserService::update($data);
    }

     //删除
     public function delete(){
        $ids = $this->request->get("ids");
        if(empty($ids)){
            return failure(config('error.er5')['code'],"请选择要删除的数据");
        }

        SysUserService::destroy($ids);
        return success();
    }

    //修改个人密码
    public function updatePwd(){
        $data = $this->request->post();
        //验证规则
        $validate = [
            'password'  => 'require|regex:/^[a-zA-Z0-9_]{8,20}$/',
            'repassword'  => 'require'
        ];
        //提示信息
        $message = [
            'password.require' => '密码不能为空!',
            'password.regex' => '密码长度8~20位，包含字母数字下划线!',
            'repassword.require' => '确认密码不能为空!',
        ];

        $this->validate($data, $validate, $message);


        //两次输入的密码不一致
        if($data['password'] !== $data['repassword']){
            return failure(config('error.er6')['code'],config('error.er6')['msg']);
        }
        $data['id'] = getUserId();
        return SysUserService::update($data);
    }


    //图片验证码
    public function verify(){
        return SysUserService::createCaptcha();
    }

    //登录
    public function login(){
        $data = $this->request->post();
        //验证规则
        $validate = [
            'account' => 'require',
            'password'  => 'require',
            'code' => 'require',
            'uniqid' => 'require',
        ];
        //提示信息
        $message = [
            'account.require' => '账号不能为空!',
            'password.require' => '密码不能为空!',
            'code.require' => '验证码不能为空!',
            'uniqid.require' => 'uniqid不能为空!',
        ];

        $this->validate($data, $validate, $message);


        $key = cache(ADMIN_LOGIN_VERIFY_.$data['uniqid']);
        if($key && password_verify(mb_strtolower($data['code'], 'UTF-8'), $key)){
            cache(ADMIN_LOGIN_VERIFY_.$data['uniqid'],null);
        }else{
            return failure(config('error.er17')['code'],config('error.er17')['msg']);
        }

        return SysUserService::login($data);
    }

}
